//
//  WSQView.m
//  WSQOCTools
//
//  Created by 翁胜琼 on 2018/8/28.
//  Copyright © 2018年 Dev John. All rights reserved.
//

#import "WSQView.h"

@interface WSQView()

@property (nonatomic,strong) WSQViewModel *viewModel;

@end

@implementation WSQView

- (instancetype)initWithViewModel:(__kindof WSQViewModel *)viewModel
{
    return [self initWithframe:self.frame viewModel:viewModel];
}

- (instancetype)initWithframe:(CGRect)frame viewModel:(__kindof WSQViewModel *)viewModel
{
    if (self = [self initWithFrame:frame])
    {
        // 只有在 ViewModel == nil的时候才给他设值
        self.viewModel = self.viewModel == nil? viewModel : self.viewModel;
        if ([self respondsToSelector:@selector(bindViewModel)])
        {
            [self bindViewModel];
        }
    }
    return self;
}

+ (instancetype)initFromXIB
{
    return [[NSBundle mainBundle]loadNibNamed:NSStringFromClass(self) owner:nil options:nil].firstObject;
}

+ (instancetype)initFromXIBWithViewModel:(__kindof WSQViewModel *)viewModel
{
    WSQView *obj;
    if((obj = (WSQView *)[self initFromXIB]))
    {
        // 只有在 ViewModel == nil的时候才设值
        obj.viewModel = obj.viewModel == nil? viewModel : obj.viewModel;
        if ([obj respondsToSelector:@selector(bindViewModel)])
        {
            [obj bindViewModel];
        }
    }
    return obj;
}

+ (instancetype)initFromXIBWithViewModel:(__kindof WSQViewModel *)viewModel withFrame:(CGRect)frame
{
    WSQView *obj;

    if((obj = (WSQView *)[self initFromXIBWithViewModel:viewModel]))
    {
        obj.frame = frame;
    }
    return obj;
}

// 最终会调用initWithFrame
- (instancetype)init
{
    self = [super init];
    if (self) {
        
    }
    return self;
}

// 在 view 中该方法是指定构造器
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        if ([self respondsToSelector:@selector(setupUI)])
        {
            [self setupUI];
        }
    }
    return self;
}

- (void)awakeFromNib
{
    [super awakeFromNib];
    [self setupUI];
}

- (void)setupUI
{
//    NSLog(@"%s",__func__);
}

- (void)bindViewModel
{
//    NSLog(@"%s",__func__);
}

@end
